package com.konne.nightmare.FastPublicOpinion.utils;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.Set;

/*字符串工具类*/
public class Utility {

    private Utility() {

    }

    public static boolean isNotEmpty(Object obj) {
        if (obj instanceof String) {
            return !isEmpty((String) obj);
        } else if (obj instanceof Long) {
            return !isEmpty((Long) obj);
        } else if (obj instanceof Integer) {
            return !isEmpty((Integer) obj);
        } else if (obj instanceof StringBuffer) {
            return !isEmpty((StringBuffer) obj);
        } else if (obj instanceof List) {
            return !isEmpty((List) obj);
        } else if (obj instanceof Set) {
            return !isEmpty((Set) obj);
        } else if (obj instanceof String[]) {
            return !isEmpty((String[]) obj);
        } else if (obj instanceof Object[]) {
            return !isEmpty((Object[]) obj);
        } else {
            return !isEmpty(obj);
        }
    }

    public static boolean equal(String s, String t) {
        if (s == null && t == null)
            return true;
        if (s != null && t != null)
            return s.trim().equals(t.trim());
        return s != null && s.equals(t);
    }

    public static boolean equal(Integer s, Integer t) {
        if (s == null && t == null)
            return true;
        return s != null && s.equals(t);
    }

    public static boolean equal(Long s, Long t) {
        if (s == null && t == null)
            return true;
        return s != null && s.equals(t);
    }


    public static boolean isEmpty(Object obj) {
        if (obj instanceof String) {
            return isEmpty((String) obj);
        } else if (obj instanceof Long) {
            return isEmpty((Long) obj);
        } else if (obj instanceof Integer) {
            return isEmpty((Integer) obj);
        } else if (obj instanceof StringBuffer) {
            return isEmpty((StringBuffer) obj);
        } else if (obj instanceof List) {
            return isEmpty((List) obj);
        } else if (obj instanceof Set) {
            return isEmpty((Set) obj);
        } else if (obj instanceof String[]) {
            return isEmpty((String[]) obj);
        } else if (obj instanceof Object[]) {
            return isEmpty((Object[]) obj);
        } else {
            return obj == null;
        }
    }


    /**
     * 校验是否是正确的IMEI号
     *
     * @param imei 设备唯一标识串码
     * @return 是否是IMEI
     */
    public static boolean isValidationIMEI(String imei) {
        char[] imeiChar = imei.toCharArray();

        if (imeiChar.length < 15) {
            return false;
        }
//        int resultInt = 0;
//        for (int i = 0; i < imeiChar.length-1; i++) {
//            int a = Integer.parseInt(String.valueOf(imeiChar[i]));
//            i++;
//            final int temp = Integer.parseInt(String.valueOf(imeiChar[i])) * 2;
//            final int b = temp < 10 ? temp : temp - 9;
//            resultInt += a + b;
//        }
//        resultInt %= 10;
//        resultInt = resultInt == 0 ? 0 : 10 - resultInt;
//        int crc= Integer.parseInt(String.valueOf(imeiChar[14]));
//        return (resultInt == crc);

        return true;
    }


    public static boolean isEmpty(String str) {
        return (null == str || str.length() == 0 || str.trim().equals("") || str.equals("null"));
    }

    public static boolean isEmpty(Long lng) {
        return (lng == null);
    }

    public static boolean isEmpty(Integer inte) {
        return (inte == null);
    }


    public static boolean isEmpty(StringBuffer stringBuffer) {
        return (stringBuffer == null || stringBuffer.length() == 0 || stringBuffer
                .toString().trim().equals(""));
    }

    public static boolean isEmpty(Object[] array) {
        return (array == null || array.length == 0);
    }

    public static boolean isEmpty(String[] array) {
        return (array == null || array.length == 0);
    }


    public static boolean isEmpty(List list) {

        return (list == null || list.isEmpty() || list.get(0) == null);
    }


    public static boolean isEmpty(Set set) {
        return (set == null || set.isEmpty());
    }

    public static Timestamp getCurrentTimestamp() {
        Calendar tmp = Calendar.getInstance();
        tmp.clear(Calendar.MILLISECOND);
        return (new Timestamp(tmp.getTime().getTime()));
    }
}
